﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.OracleClient;
using CardLogistics.Model.CardRecovery;

namespace CardLogistics.DAL.CardRecovery
{
    /// <summary>
    /// 未售问题卡回收明细_数据访问类
    /// </summary>
    public class NotSaleCard_DetailData
    {
        #region 查询
        /// <summary>
        /// 根据条件查询相应的表的字段
        /// </summary>
        /// <param name="StrWhere">查询条件</param>
        /// <param name="StrFields">要查询的字段</param>
        /// <returns>填充完数据的DataTable</returns>
        public DataTable GetList(string StrWhere, string StrFields)
        {
            return CommonData.GetListOrderBy(StrWhere, "v_NotSaleCard_Detail", StrFields, "StartCardNo,EndCardNo", 0);
        }

        /// <summary>
        /// 根据编号查询基本信息
        /// </summary>
        /// <param name="id">编号</param>
        /// <returns>填充完数据的DataTable</returns>
        public DataTable GetInfoById(string id)
        {
            return GetList(" ID = '" + id + "' ", "*");
        }


        /// <summary>
        /// 通过未售问题卡外键查询基本信息
        /// </summary>
        /// <param name="recoveryId">未售问题卡外键ID</param>
        /// <returns></returns>
        public DataTable GetInfoByRecoveryId(string recoveryId)
        {
            return GetList(" RecoveryId = '" + recoveryId + "' ", "*");
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public NotSaleCard_Detail GetModel(string rId)
        {
            NotSaleCard_Detail model = new NotSaleCard_Detail();
            DataRow row = GetInfoById(rId).Rows[0];
            if (row != null)
            {
				if(row["ID"]!=null)
				{
					model.ID=row["ID"].ToString();
				}
				if(row["RECOVERYID"]!=null)
				{
					model.RECOVERYID=row["RECOVERYID"].ToString();
				}
				if(row["STARTCARDNO"]!=null && row["STARTCARDNO"].ToString()!="")
				{
					model.STARTCARDNO=row["STARTCARDNO"].ToString();
				}
				if(row["ENDCARDNO"]!=null && row["ENDCARDNO"].ToString()!="")
				{
					model.ENDCARDNO=row["ENDCARDNO"].ToString();
				}

				if(row["CARDAMOUNT"]!=null && row["CARDAMOUNT"].ToString()!="")
				{
					model.CARDAMOUNT=decimal.Parse(row["CARDAMOUNT"].ToString());
				}
            }
            return model;
        }
        #endregion

        #region 新增
        /// <summary>
        /// 新增信息
        /// </summary>
        /// <param name="model">实体类</param>
        /// <returns>返回插入数据的编号</returns>
        public string Add(NotSaleCard_Detail model)
        {
            try
            {
                OracleParameter[] parameters = {
                    new OracleParameter("varOut", OracleType.VarChar,50),
				 	new OracleParameter("vRECOVERYID", OracleType.NVarChar),
					new OracleParameter("vSTARTCARDNO", OracleType.VarChar,10),
					new OracleParameter("vENDCARDNO", OracleType.VarChar,10),
					new OracleParameter("vCARDAMOUNT", OracleType.Number,4)};
                parameters[0].Direction = ParameterDirection.Output;
                parameters[1].Value = model.RECOVERYID;
                parameters[2].Value = model.STARTCARDNO;
                parameters[3].Value = model.ENDCARDNO;
                parameters[4].Value = model.CARDAMOUNT;
                string StrOutPar = DbHelperSQL.RunProcedure("P_NotSaleCard_Detail_Add", parameters, parameters[0]).ToString();
                return StrOutPar;
            }
            catch (Exception ex)
            {
                return "err," + ex.Message;
            }
        }
        #endregion

        #region 修改
        /// <summary>
        /// 修改信息
        /// </summary>
        /// <param name="model">实体类</param>
        /// <returns>返回插入数据的编号</returns>
        public string Update(NotSaleCard_Detail model)
        {
            try
            {
                OracleParameter[] parameters = {
                    new OracleParameter("vID", OracleType.VarChar,50),
					new OracleParameter("vRECOVERYID", OracleType.NVarChar),
					new OracleParameter("vSTARTCARDNO", OracleType.VarChar,10),
					new OracleParameter("vENDCARDNO", OracleType.VarChar,10),
					new OracleParameter("vCARDAMOUNT", OracleType.Number,4),
                    new OracleParameter("varOut", OracleType.VarChar,50)};
                parameters[0].Value = model.ID;
                parameters[1].Value = model.RECOVERYID;
                parameters[2].Value = model.STARTCARDNO;
                parameters[3].Value = model.ENDCARDNO;
                parameters[4].Value = model.CARDAMOUNT;
                parameters[5].Direction = ParameterDirection.Output;
                string StrOutPar = DbHelperSQL.RunProcedure("P_NotSaleCard_Detail_Update", parameters, parameters[5]).ToString();
                return StrOutPar;
            }
            catch (Exception ex)
            {
                return "err," + ex.Message;
            }
        }
        #endregion

        #region 删除
        /// <summary>
        /// 删除信息
        /// </summary>
        /// <param name="id">删除信息的ID</param>
        /// <returns>删除成功返回True。否则返回false</returns>
        public string Delete(string id)
        {
            try
            {
                OracleParameter[] parameters = {
                        new OracleParameter("varOut",  OracleType.VarChar,100),
					    new OracleParameter("vid",  OracleType.VarChar,50)
                };
                parameters[0].Direction = ParameterDirection.Output;
                parameters[1].Value = id;
                string StrOutPar = DbHelperSQL.RunProcedure("P_NotSaleCard_Detail_Delete", parameters, parameters[0]).ToString();
                return StrOutPar;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion
    }
}
